/*
 *		source/editor/editor.js - A simple text editor
 */

/* Tegi's Commander - A file manager for Palm WebOS
 * Copyright (C) 2011 Timo Tegtmeier
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

enyo.kind({
	name:		"TextEditor",
	kind:		"Control",
	nodeTag:	"textarea",
	style:		"font-size: 16px; font-family: Courier, monospace; width: 100%; border: none;",

	published: {
		value:				"",
		spellcheck: 		true,
		autocorrect: 		true,
		autoKeyModifier:	"",
		autoCapitalize:		"sentence",
		autoLinking: 		false,
		autoWordComplete:	true
	},

	getValue: function() {
		if(this.hasNode()) {
			this.value = this.node.value;
		}
		return this.value;
	},

	setValue: function(newValue) {
		this.value = newValue;
		if(this.hasNode()) {
			this.node.value = this.value;
		}
	},

	rendered: function() {
		this.inherited(arguments);
		if(this.hasNode()) {
			this.node.value = this.value;
		}
	},

	autoKeyModifierChanged: function() {
		this.setAttribute("x-palm-text-entry", this.autoKeyModifier ? this.autoKeyModifier : null);
	},

	autoCapitalizeChanged: function() {
		this.setAttribute("x-palm-disable-auto-cap", this.autoCapitalize == "lowercase" ? true : null);
		this.setAttribute("x-palm-title-cap", this.autoCapitalize == "title" ? true : null);
	},

	autocorrectChanged: function() {
		this.setAttribute("autocorrect", this.autocorrect ? "on" : "off");
	},

	spellcheckChanged: function() {
		this.setAttribute("spellcheck", this.spellcheck ? true : false);
	},

	autoLinkingChanged: function() {
		this.setAttribute("x-palm-enable-linker", this.autoLinking ? this.autoLinking : null);
	},

	autoWordCompleteChanged: function() {
		this.setAttribute("x-palm-word-completions", this.autoWordComplete ? null : "disabled");
	},

	create: function() {
		this.inherited(arguments);

		this.autoKeyModifierChanged();
		this.autoCapitalizeChanged();
		this.autocorrectChanged();
		this.spellcheckChanged();
		this.autoLinkingChanged();
		this.autoWordCompleteChanged();
	}
});

enyo.kind({
	name:	"Editor",
	kind:	"VFlexBox",

	path:	"",

	components:	[{
		name:		"header",
		kind:		"Toolbar",
		className:	"enyo-toolbar-light",
		layoutKind:	"HFlexLayout",
		components:	[{
			name:		"title",
			kind:		"Control",
			className:	"title ellipsis",
			flex:		1
		}, {
			name:		"headerSaveButton",
			kind:		"ToolButton",
			icon:		"../../images/save.png",
			onclick:	"saveFile"
		}]
	}, {
		kind:		"Scroller",
		flex:		1,
		style:		"background-color: white",
		components:	[{
			name:			"editor",
			kind:			"TextEditor",
			oninput:		"fileChanged",
			autocorrect:	false
		}]
	}, {
		kind:		"Toolbar",
		components:	[{
			name:		"saveButton",
			kind:		"ToolButton",
			icon:		"../../images/save.png",
			onclick:	"saveFile"
		}, {
			kind:		"ToolButton",
			icon:		"../../images/refresh.png",
			onclick:	"refreshFile"
		}, {
			kind:		"Spacer"
		}]
	}, {
		name:		"licenseDialog",
		kind:		"LicenseDialog"
	}, {
		name:		"helpDialog",
		kind:		"HelpDialog"
	}, {
		name:					"reReadDialog",
		kind: 					"DialogPrompt",
		title: 					$L("Do you want to refresh this file?"),
		message: 				$L("Refreshing this file will abandon all changes made so far."),
		acceptButtonCaption: 	$L("Yes, abandon changes"),
		cancelButtonCaption: 	$L("Cancel"),
		onAccept: 				"readFile"
	}, {
		kind: 		"Dialog",
		name:		"errorDialog",
		lazy:		false,
		components: [{
			name:		"errorMessage",
			style:		"margin: 25px"
		}, {
			kind: 	"HFlexBox",
			pack: "center",
			components: [{
	            kind:		"Button",
				caption:	$L("OK"),
				className:	"enyo-button-affirmative",
				style:		"margin: 0px 25px 10px 25px",
				flex:		1,
				onclick:	"closeDialog"
			}]
		}]
	}, {
		kind:					"AppMenu",
		components:				[{
			kind:				"EditMenu"
		}, {
			caption:			$L("License"),
			onclick:			"openLicense"
		}, {
			caption:			$L("Help"),
			onclick:			"openHelp"
		}]
	}, {
		name:		"fileService",
		kind:		"PalmService",
		service:	"palm://com.tegistuff.app.commander.service/"
	}, {
		kind:		"ApplicationEvents",
		onUnload:	"saveFile"
	}],

	//
	// Dialog handling
	//

	showError: function(error) {
		this.$.errorMessage.setContent(error || $L("File operation failed."));
		this.$.errorDialog.open();
	},

	closeDialog: function(sender, event) {
		this.$.errorDialog.close();
	},

	//
	// File handling
	//

	refreshFile: function() {
		if(this.$.saveButton.disabled) {
			this.readFile();
		} else {
			this.$.reReadDialog.open();
		}
	},

	readFile: function() {
		this.$.fileService.call({
			path:		this.path
		}, {
			method:		"readfile",
			onSuccess:	"readFileSuccess",
			onFailure:	"readFileFailed"
		});
	},

	readFileSuccess: function(sender, response) {
		this.$.editor.setValue(response.data);
		this.$.saveButton.setDisabled(true);
		this.$.headerSaveButton.setDisabled(true);
	},

	readFileFailed: function() {
		this.showError($L("Unable to read the file. Maybe you don't have the permissions to read it."));
	},

	saveFile: function() {
		if(!this.$.saveButton.disabled) {
			this.$.fileService.call({
				path:		this.path,
				data:		this.$.editor.getValue()
			}, {
				method:		"writefile",
				onSuccess:	"saveFileSuccess",
				onFailure:	"saveFileFailed"
			});
		}
	},

	saveFileSuccess: function() {
		this.$.saveButton.setDisabled(true);
		this.$.headerSaveButton.setDisabled(true);
	},

	saveFileFailed: function() {
		this.showError($L("Unable to save the file. Maybe you don't have the permissions to save it."));
	},

	fileChanged: function() {
		if(this.$.saveButton.disabled) {
			this.$.saveButton.setDisabled(false);
			this.$.headerSaveButton.setDisabled(false);
		}
	},

	//
	// Menu handling
	//

	openHelp: function() {
		enyo.asyncMethod(this, function() {
			this.$.helpDialog.openAtCenter();
		});
	},

	openLicense: function() {
		enyo.asyncMethod(this, function() {
			this.$.licenseDialog.openAtCenter();
		});
	},

	//
	// Initialization
	//

	create: function() {
		this.inherited(arguments);

		enyo.keyboard.setResizesWindow(false);
		this.path = enyo.windowParams.path;

		this.$.title.setContent($L("Editor") + " - " + this.path);

		this.readFile();
	}
});
